package com.yumeisoft.aitables.dialog;

import com.yumeisoft.aitables.entity.DatabaseConfigVO;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.function.Predicate;

public class DatabaseConfigDialog extends JDialog {
    private JTextArea promptField;
    private JTextField mysqlHostField;
    private JTextField portField;
    private JTextField databaseNameField;
    private JTextField usernameField;
    private JPasswordField passwordField;

    public void showDialog(Predicate<DatabaseConfigVO> callback) {
        // 设置对话框属性
        this.setTitle("AITables:任务配置");
        this.setSize(400, 300);
        this.setLocationRelativeTo(null); // 居中显示

        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.anchor = GridBagConstraints.WEST;
        constraints.insets = new Insets(5, 5, 5, 5);

        int row = 0;

        // 添加提示词
        JLabel promptLabel = new JLabel("AI提示词:");
        constraints.gridx = 0; constraints.gridy = row++;
        panel.add(promptLabel, constraints);
        promptField = new JTextArea(5,20);
        promptField.setAutoscrolls(true);
        promptField.setLineWrap(true);
        promptField.setText("我想做一个图书馆管理系统，支持手机端、电脑端和APP，要有通知提醒功能");
        constraints.gridx = 1;
        panel.add(promptField, constraints);

        // 添加MySQL地址
        JLabel mysqlHostLabel = new JLabel("MySQL主机:");
        constraints.gridx = 0; constraints.gridy = row++;
        panel.add(mysqlHostLabel, constraints);
        mysqlHostField = new JTextField(20);
        mysqlHostField.setText("localhost");
        constraints.gridx = 1;
        panel.add(mysqlHostField, constraints);

        // 添加端口号
        JLabel portLabel = new JLabel("端口:");
        constraints.gridx = 0; constraints.gridy = row++;
        panel.add(portLabel, constraints);
        portField = new JTextField(5);
        portField.setText("3306");
        constraints.gridx = 1;
        panel.add(portField, constraints);

        // 添加数据库名称
        JLabel databaseNameLabel = new JLabel("数据库:");
        constraints.gridx = 0; constraints.gridy = row++;
        panel.add(databaseNameLabel, constraints);
        databaseNameField = new JTextField(20);
        databaseNameField.setText("test");
        constraints.gridx = 1;
        panel.add(databaseNameField, constraints);

        // 添加用户名
        JLabel usernameLabel = new JLabel("用户名:");
        constraints.gridx = 0; constraints.gridy = row++;
        panel.add(usernameLabel, constraints);
        usernameField = new JTextField(20);
        usernameField.setText("root");
        constraints.gridx = 1;
        panel.add(usernameField, constraints);

        // 添加密码
        JLabel passwordLabel = new JLabel("密码:");
        constraints.gridx = 0; constraints.gridy = row++;
        panel.add(passwordLabel, constraints);
        passwordField = new JPasswordField(20);
        constraints.gridx = 1;
        panel.add(passwordField, constraints);

        JPanel buttonPanel = new JPanel();
        JButton okButton = new JButton("立即开始");
        JButton cancelButton = new JButton("取消");

        okButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 验证输入
                if (promptField.getText().isEmpty() || mysqlHostField.getText().isEmpty() ||
                        portField.getText().isEmpty() || databaseNameField.getText().isEmpty() ||
                        usernameField.getText().isEmpty() || passwordField.getPassword().length == 0) {
                    JOptionPane.showMessageDialog(DatabaseConfigDialog.this, "Please fill in all fields!", "Error", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                int port = -1;
                try {
                    port = Integer.parseInt(portField.getText());
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(DatabaseConfigDialog.this, "Port must be a number!", "Error", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                String password = new String(passwordField.getPassword());

                DatabaseConfigVO config = new DatabaseConfigVO();
                config.setPrompt(promptField.getText());
                config.setMysqlHost(mysqlHostField.getText());
                config.setPort(port);
                config.setDatabaseName(databaseNameField.getText());
                config.setUsername(usernameField.getText());
                config.setPassword(password);

                // 设置结果并关闭对话框
                callback.test(config);
                DatabaseConfigDialog.this.dispose();
            }
        });

        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DatabaseConfigDialog.this.setVisible(false);
            }
        });

        buttonPanel.add(okButton);
        buttonPanel.add(cancelButton);

        getContentPane().add(panel, BorderLayout.CENTER);
        getContentPane().add(buttonPanel, BorderLayout.SOUTH);

        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        this.pack();
        this.setVisible(true);
    }
}
